﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;
using System.Data.Objects;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfScheduleQuery : IScheduleQuery
    {
        public IEnumerable<Schedule> GetAllSchedule()
        {
            var context = new hmsDBEntities();
            return context.scheduleDetails.Select(f => new Schedule()
            {
                scheduleID = f.scheduleID,
                roomNum = f.roomNum,
                staffID = f.staffID,
                scheduledDateTime = f.scheduledDateTime

            });


        }
        public void AddSchedule(string scheduleID, int roomNum, string staffID, DateTime scheduledDateTime)
        {
            var context = new hmsDBEntities();
            var newSchedule = context.scheduleDetails.Create();
            newSchedule.scheduleID = scheduleID;
            newSchedule.roomNum = roomNum;
            newSchedule.staffID = staffID;
            newSchedule.scheduledDateTime = scheduledDateTime;
            context.scheduleDetails.Add(newSchedule);
            context.SaveChanges();
        }

        public void RetrieveSchedule(string scheduleID)
        {
            var context = new hmsDBEntities();
            var scheduleToRetrieve = context.scheduleDetails.FirstOrDefault(s => s.scheduleID == scheduleID);
            context.scheduleDetails.Remove(scheduleToRetrieve);
            context.SaveChanges();
        }

        public void DeleteSchedule(string scheduleID)
        {
            var context = new hmsDBEntities();
            var scheduleToDelete = context.scheduleDetails.FirstOrDefault(s => s.scheduleID == scheduleID);
            context.scheduleDetails.Remove(scheduleToDelete);
            context.SaveChanges();
        }

        public void UpdateSchedule(string scheduleID, int roomNum, string staffID, DateTime scheduledDateTime)
        {
            var context = new hmsDBEntities();
            var scheduleToUpdate = context.scheduleDetails.FirstOrDefault(s => s.scheduleID == scheduleID);
            scheduleToUpdate.scheduleID = scheduleID;
            scheduleToUpdate.roomNum = roomNum;
            scheduleToUpdate.staffID = staffID;
            scheduleToUpdate.scheduledDateTime = scheduledDateTime;

            context.Entry(scheduleToUpdate).State = System.Data.EntityState.Modified;
            context.SaveChanges();

        }
    }
}